package base.email;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.lxit.cms.entity.main.CmsUser;
import com.lxit.cms.manager.main.CmsUserMng;

/**
 * Servlet implementation class SendEmailAndInfo
 */
@Controller
public class SendEmailAndInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	public SendEmailAndInfo() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	  String result = "发送成功";
	  setServlet();//*********得到Spring容器***********
	  request.setCharacterEncoding("UTF-8");
	  response.setCharacterEncoding("UTF-8");
	  String userId = request.getParameter("user_id");
    if(userId.equals("") || userId == null ) {
      userId = "0";
      result = "发送失败";
    }
    String emailTo = null;
	  CmsUser cmsUser = getCmsUserMng().findById(Integer.parseInt(userId));
	  if(cmsUser != null) {
	    emailTo = cmsUser.getEmail();//接收信息党员的email
	  }
	  cmsUser = getCmsUserMng().findByUsername("admin");
    String emailFrom = cmsUser.getEmail();//管理员email
    String title = request.getParameter("title");//信息标题
    String content = request.getParameter("content");//信息内容
    Map<String,String> hashMap = new HashMap<String,String>();
    hashMap.put("emailTo", emailTo);
    hashMap.put("emailFrom", emailFrom);
    hashMap.put("title", title);
    hashMap.put("content", content);
	  try {
      sendEmail(hashMap);
    } catch (Exception e) {
      e.printStackTrace();
      result = "发送失败";
    }
    try {
      response.setCharacterEncoding("utf-8");
      response.setContentType("text/html");
      response.getWriter().print("{result:'"+result+"'}");
    } catch (IOException e) {
      e.printStackTrace();
    }
	}
  public void sendEmail(Map<String,String> map) throws Exception {
    SpringMail springMail = new SpringMail(wac);
    springMail.sendTextMail(springMail.sender,map);
  }
  public void setServlet( ) {
      ServletContext servletContext =getServletContext();;
      setWac(WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext));
      setCmsUserMng((CmsUserMng)getWac().getBean("cmsUserMng"));
  }
  private WebApplicationContext wac;
  
  public void setCmsUserMng(CmsUserMng cmsUserMng) {
    this.cmsUserMng = cmsUserMng;
  }

  public CmsUserMng getCmsUserMng() {
    return cmsUserMng;
  }
  public void setWac(WebApplicationContext wac) {
    this.wac = wac;
  }

  public WebApplicationContext getWac() {
    return wac;
  }
  private CmsUserMng cmsUserMng;

  
 
}
